package com.woniuxy.controller.goodAttribute;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.dto.GoodsPropGroupDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsSmallDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsSmallerDto;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.GoodsFiledException;
import com.woniuxy.exception.PawnRoleException;
import com.woniuxy.param.*;
import com.woniuxy.service.GoodsPropgroupService;
import com.woniuxy.service.GoodsSmallService;
import com.woniuxy.service.GoodsSmallerService;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 属性组 前端控制器
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/goods-propgroup")
@Api(tags = "属性组接口")
public class GoodsPropgroupController {

    @Resource
    private GoodsPropgroupService service;
    @Resource
    private GoodsSmallService goodsSmallService;
    @Resource
    private GoodsSmallerService goodsSmallerService;

    /**
     * 修改数组数据
     *
     * @param ugpp
     * @return
     */
    @ApiOperation("修改数组数据")
    @PostMapping("/updateGoodsPropGroup.do")
    public Result updateGoodsPropGroup(@RequestBody UpdateGoodsPropgroupParam ugpp) {

        if (service.updateGoodsPropGroup(ugpp)) {
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }
        throw new PawnRoleException(CodeEnum.GOODS_PROP_GROUP_UPDATE_FAIL_2.getMessage(), CodeEnum.GOODS_PROP_GROUP_UPDATE_FAIL_2.getCode());
    }

    /**
     * 批量删除属性组(逻辑删除)
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除属性组(逻辑删除)")
    @PostMapping("/removePropGroupByIds.do")
    public Result removePropGroupByIds(@RequestBody List<Integer> ids) {
        if (service.removePropGroupByIds(ids)) {
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }
        throw new PawnRoleException(CodeEnum.GOODS_PROP_GROUP_DELETE_FAIL.getMessage(), CodeEnum.GOODS_PROP_GROUP_DELETE_FAIL.getCode());
    }

    /**
     * 添加新属性组
     *
     * @param sgpp
     * @return
     */
    @ApiOperation("批量添加新属性组")
    @PostMapping("/saveGoodsPropGroups.do")
    public Result saveGoodsPropGroups(@RequestBody @Validated SaveGoodsPropgroupParam sgpp, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        if (service.saveGoodsPropGroups(sgpp))
            return Result.newInstance().setCode(200).setMsg("添加新属性成功");
        else {
            throw new GoodsFiledException(CodeEnum.GOODS_PROP_SAVE_FAIL.getMessage(), CodeEnum.GOODS_PROP_SAVE_FAIL.getCode());

        }
    }

    /**
     * 分页查询所有属性组
     *
     * @param pqp
     * @return
     */
    @ApiOperation("分页查询所有属性组")
    @PostMapping("/listGoodsPropGroup.do")
    public Result listGoodsPropGroup(@RequestBody PageQueryParam<String> pqp) {
        BaseDto<GoodsPropGroupDto> goodsPropGroupDtoBaseDto = service.listGoodsPropGroup(pqp);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(goodsPropGroupDtoBaseDto);
    }


    /**
     * 查询所有小类
     *
     * @return
     */
    @ApiOperation("查询所有小类")
    @PostMapping("/listGoodsSmall.do")
    public Result listGoodsSmall() {
        List<GoodsSmallDto> goodsSmallDtos = goodsSmallService.listGoodsSmall();
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(goodsSmallDtos);
    }

    /**
     * 查询所有三级类
     *
     * @return
     */
    @ApiOperation("查询所有小类")
    @PostMapping("/listGoodsSmaller.do")
    public Result listGoodsSmaller() {
        List<GoodsSmallerDto> dtos = goodsSmallerService.listGoodsSmaller();
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(dtos);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询当前属性组")
    @GetMapping("/findGoodsPropGroupById/{id}")
    @ApiImplicitParam(name = "id", value = "属性组id")
    public Result findGoodsPropGroupById(@PathVariable Integer id) {
        GoodsPropGroupDto dto = service.findGoodsPropGroupById(id);
        return Result.newInstance().setMsg("查询成功").setCode(200).setData(dto);
    }


}

